package com.hanson.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.DeleteParam;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.service.RedisService;
import com.hanson.common.util.BusinessUtil;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.GoodsBean;
import com.hanson.entity.OrderBean;
import com.hanson.entity.UserBean;
import com.hanson.service.OrderService;
import com.hanson.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap,HttpServletRequest request) {
        String token = BusinessUtil.getCookieToken(request, "token");
        Integer merId = -1;
        if (StringUtils.isNotEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (null != userBean) {
                merId = userBean.getId();
                modelMap.put("userType",userBean.getUserType());
            }
        }
        modelMap.put("order", orderService.statisOrder(merId));
        return "admin/order/index";
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(ModelMap modelMap,String id) {
        modelMap.put("data",orderService.selectDetail(id));
        return "admin/order/detail";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(ModelMap modelMap,String id) {
        modelMap.put("data",orderService.selectDetail(id));
        return "admin/order/modify";
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<OrderBean> listData(int limit, int page, OrderBean orderBean, HttpServletRequest request) {
        String token = BusinessUtil.getCookieToken(request, "token");
        Integer merId = -1;
        if(StringUtils.isNotEmpty(token)){
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if(null != userBean){
                merId = userBean.getId();
            }
        }
        orderBean.setMerId(merId);
        Page<OrderBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(orderService.listOrders(pageInfo, orderBean));
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modifyData(OrderBean orderBean) {
        ResultMessage resultMessage = new ResultMessage();
        orderService.updateOrderInfo(orderBean);
        resultMessage.setMsg("修改订单信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(@RequestBody DeleteParam deleteParam) {
        ResultMessage resultMessage = new ResultMessage();
        orderService.deleteOrders(deleteParam.getIds());
        resultMessage.setMsg("删除订单成功！");
        return resultMessage;
    }


}
